package pl.soulmusic.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pl.soulmusic.data.model.POIs;

public class SynchronizeData {

    private static final Log log = LogFactory.getLog(SynchronizeData.class);

    public SynchronizeData() {

    }

    public String addPOI(String login, String passwordHash, String name,
	    String description, double latitude, double longitude,
	    int maxDistance) {
	try {
	    int userID = StorageUtils.existUser(login, passwordHash);
	    if (userID > 0) {
		StorageUtils.addNewPOI(name, description, latitude, longitude,
			maxDistance, userID);
	    } else {
		return "Użytkownik nie istnieje";
	    }
	} catch (Exception e) {
	    log.error("Cos sie zesrało:", e);
	    return "Błąd podczas dodawania";
	}
	return "Dodano";
    }

    public String addTrack(String login, String passwordHash, double longitude, double latitude ,
	    String genreName, String albumName, String albumYear,
	    String artistName, String artistCountry, String trackTitle,
	    String trackYouUrl) {
	try {
	    int userID = StorageUtils.existUser(login, passwordHash);
	    if (userID > 0) {// User exist
		
		    int genreID=-1;
		    if(!genreName.equals("")){
			genreID=StorageUtils.getOrAddGenre(genreName);
		    }
		    int artistID=-1;
		    if(artistName.equals("")){
			return "Nazwa artysty jest niezbędna";
		    }
		    artistID=StorageUtils.getOrAddArtist(artistName,artistCountry);
		    int albumID=-1;
		    if(!albumName.equals("")){
			albumID=genreID=StorageUtils.getOrAddAlbum(albumName, albumYear,artistID);
		    }
		    int trackID=-1;
		    if(trackTitle.equals("")){
			return "Nazwa ścieżki jest niezbędna";
		    }
		    trackID=StorageUtils.getOrAddTrack(trackTitle,trackYouUrl, artistID,genreID,albumID);
		    int listTrackId=StorageUtils.addListenedTrack(new Timestamp(new Date().getTime()),latitude,longitude,trackID,userID);
		    List<POIs> list =StorageUtils.getPOIs(userID);
		    for(POIs poi:list){
			log.warn("Dist:"+poi.getName()+"km:"+haversine_km(poi.getLatitude(), poi.getLongitude(), latitude, longitude));
			if(haversine_km(poi.getLatitude(), poi.getLongitude(), latitude, longitude)<poi.getMax_distance())
		    StorageUtils.addPOITrack(listTrackId, poi.getId());
		    }
	    } else {
		return "Użytkownik nie istnieje";
	    }
	} catch (Exception e) {
	    log.error("Cos sie zesrało:", e);
	    return "Błąd podczas dodawania";
	}
	return "Dodano";
    }
    private double haversine_km(double lat1, double long1, double lat2, double long2)
    {
	double d2r= (Math.PI / 180.0);
        double dlong = (long2 - long1) * d2r;
        double dlat = (lat2 - lat1) * d2r;
        double a = Math.pow(Math.sin(dlat/2.0), 2) + Math.cos(lat1*d2r) * Math.cos(lat2*d2r) * Math.pow(Math.sin(dlong/2.0), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double d = 6367 * c;

        return d;
    }


}
